package com.edu.control.util;

import com.edu.control.deploy.constant.Constants;
import com.edu.control.model.DeployRequest;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

public class YamlUtil {

    public static void main(String[] args) throws Exception {
        DeployRequest deployRequest = new DeployRequest();
        File file = new File("E:\\dev_file\\git\\lowcode\\function_event-1");
        Yaml yaml = modifyYaml(deployRequest, "", "http://baidu.com", file);
        System.out.println(yaml);
    }

    public static Yaml modifyYaml(DeployRequest deployRequest, String imageRepository, String openfaasGateway, File file) throws Exception {
        Yaml yaml = new Yaml();
        Map templateMap = yaml.loadAs(YamlUtil.class.getClassLoader().getResourceAsStream("template/openfaas/deploy/" + deployRequest.getBootConfig().getLang() + "/deploy.yml"), Map.class);

        Map provider = (Map) templateMap.get("provider");
        provider.put("gateway", openfaasGateway);
        Map functionDetail = (Map) ((Map) templateMap.get("functions")).get("functionName");
        ((Map) templateMap.get("functions")).put(deployRequest.getFunctionName(), functionDetail);
        ((Map) templateMap.get("functions")).remove("functionName");
        functionDetail = (Map) ((Map) templateMap.get("functions")).get(deployRequest.getFunctionName());
        functionDetail.put("namespace", deployRequest.getBootConfig().getNamespace());
        functionDetail.put("image", imageRepository + "/" + deployRequest.getFunctionName() + ":" + deployRequest.getDeployVersion());
        functionDetail.put("handler", "./" + deployRequest.getFunctionName());
        Map memoryMap = new HashMap(1);
        memoryMap.put("memory", deployRequest.getBootConfig().getMemory() + "Mi");
        memoryMap.put("cpu", deployRequest.getBootConfig().getCpu() + "m");
        functionDetail.put("limits", memoryMap);
        Map requestsMap = new HashMap(1);
        requestsMap.put("cpu", (deployRequest.getBootConfig().getCpu() / 2) + "m");
        requestsMap.put("memory", deployRequest.getBootConfig().getMemory() / 2 + "Mi");
        functionDetail.put("requests", requestsMap);
        Map envVars = (Map) functionDetail.get("environment");
        envVars.put("read_timeout", deployRequest.getBootConfig().getTimeout() + "s");
        envVars.put("write_timeout", deployRequest.getBootConfig().getTimeout() + "s");
        envVars.put("upstream_timeout", deployRequest.getBootConfig().getTimeout() + "s");

        Map labels = (Map) functionDetail.get("labels");
        if (deployRequest.getBootConfig().getLabelMap() != null) {
            labels.putAll(deployRequest.getBootConfig().getLabelMap());
        }

        if (deployRequest.getBootConfig().getAnnotationMap() != null) {
            Map annotations = (Map) functionDetail.get("annotations");
            annotations.putAll(deployRequest.getBootConfig().getLabelMap());
        }

        Map<String, Object> scale = new HashMap<>();
        Integer replicas = deployRequest.getBootConfig().getReplicas();
        scale.put("com.openfaas.scale.max", replicas == null || replicas < 0 || replicas > 10 ? 1 : replicas);
        scale.put("com.openfaas.scale.min", 1);
        scale.put("com.openfaas.scale.factor", 20);
        scale.put("watchName", deployRequest.getFunctionName() + Constants.SPLIT + deployRequest.getDeployVersion());
        labels.putAll(scale);
        DumperOptions dumperOptions = new DumperOptions();
        dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml yamlDump = new Yaml(dumperOptions);
        File dumpfile = new File(file, deployRequest.getFunctionName() + ".yml");
        try (FileWriter fileWriter = new FileWriter(dumpfile)) {
            yamlDump.dump(templateMap, fileWriter);
        }
        return yamlDump;
    }
}
